package org.shopen.puremvc.view
{
	import flash.display.Stage;
	import flash.display.StageDisplayState;
	import flash.events.KeyboardEvent;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	import org.shopen.puremvc.ApplicationConstants;

	public class FullscreenMediator extends Mediator
	{
		public static const NAME : String = "FullscreenMediator";
		
		protected function get stage() : Stage
		{
			return viewComponent as Stage;
		}
		
		public function FullscreenMediator(viewComponent:Object=null)
		{
			super(FullscreenMediator.NAME, viewComponent);
		}
		
		override public function onRegister():void
		{
			addListeners();
		}
		
		override public function getMediatorName():String
		{
			return FullscreenMediator.NAME;
		}
		
		override public function listNotificationInterests():Array
		{
			return [ApplicationConstants.TOGGLE_FULLSCREEN];
		}
		
		override public function handleNotification(notification:INotification):void
		{			
			switch(notification.getName())
			{
				case ApplicationConstants.TOGGLE_FULLSCREEN:
					toggleFullscreen();
					break;
			}
		}
		
		protected function addListeners() : void
		{
			stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownListener);
		}
		
		private function keyDownListener(event : KeyboardEvent) : void
		{
			var char : String = String.fromCharCode(event.charCode).toLowerCase();
			if (event.ctrlKey && char == "f")
			{
				toggleFullscreen();
			}
		}
		
		protected function toggleFullscreen() : void
		{
			try
			{
                    switch (stage.displayState) {
                        case StageDisplayState.FULL_SCREEN:
                            stage.displayState = StageDisplayState.NORMAL;
                            break;
                        default:
                            stage.displayState = StageDisplayState.FULL_SCREEN;
                            break;
                    }
             } 
             catch (error : SecurityError)
             {
                   // ignore
             }
		}
	}
}